package com.google.gdata.data.douban;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.xml.sax.Attributes;

import com.google.gdata.data.ExtensionDescription;
import com.google.gdata.data.ExtensionPoint;
import com.google.gdata.data.ExtensionProfile;
import com.google.gdata.data.Link;
import com.google.gdata.data.Person;
import com.google.gdata.data.TextConstruct;
import com.google.gdata.data.Link.AtomHandler;  
import com.google.gdata.data.extensions.Rating; 
import com.google.gdata.util.Namespaces;
import com.google.gdata.util.ParseException;
import com.google.gdata.util.XmlParser;
import com.google.gdata.util.XmlParser.ElementHandler;

@ExtensionDescription.Default(nsAlias = "atom", nsUri = Namespaces.atom, localName = Author.XML_NAME)
public class Author extends Person {
	/** XML element name */
	static final String XML_NAME = "author";
	private List<Link> links = new ArrayList<Link>();

	public List<Link> getLinks() {
		return links;
	}

	  public Link getLink(String rel, String type) {

	    for (Link link : links) {
	      if (link.matches(rel, type)) {
	        return link;
	      }
	    }

	    return null;
	 }
	  
	  
	public void setLinks(List<Link> links) {
		this.links = links;
	}

	public Author() {
		super();
	}

	@Override
	public void declareExtensions(ExtensionProfile extProfile) {
		if (extProfile.isDeclared(Author.class)) {
			return;
		}
		super.declareExtensions(extProfile);
	}

	@Override
	protected void validate() {
	}
	 
	@Override
	public XmlParser.ElementHandler getHandler(ExtensionProfile extProfile,
			String namespace, String localName, Attributes attrs)
			throws ParseException {

		try {
			return new Handler(extProfile);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private class Handler extends Person.ExtensionHandler {

		public Handler(ExtensionProfile extProfile) throws ParseException,
				IOException {

			super(extProfile, Author.class);
		}

		public ElementHandler getChildHandler(String namespace,
				String localName, Attributes attrs) throws ParseException,
				IOException {
			if (localName.equals("link")) {
				Link link = new Link();
				links.add(link);
				return link.new AtomHandler(extProfile);  
			} else { 
				return super.getChildHandler(namespace, localName, attrs); 
			}
		}
 
	}


	public static ExtensionDescription getDefaultDescription(boolean required,
			boolean repeatable) {
		ExtensionDescription desc = ExtensionDescription
				.getDefaultDescription(Author.class);
		desc.setRequired(required);
		desc.setRepeatable(repeatable);
		return desc;
	}
	
	
	  

}
